package com.ucode.cgms.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ucode.cgms.mapper.CgmsCompanionProductMapper;
import com.ucode.model.cgms.CgmsCompanionProduct;
import com.ucode.model.cgms.enums.CgmsSysResultCodeEnum;
import com.ucode.springboot.starter.mybatis.core.base.BaseService;
import com.ucode.tool.base.enums.CommonReleaseStatusEnum;
import com.ucode.tool.base.enums.GlobalResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.tool.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 陪玩服务商品Service
 * @Author: liliang
 * @Date: 2025/5/9 18:09
 * @Version: 1.0
 */
public interface CgmsCompanionProductService extends BaseService<Long, CgmsCompanionProduct,CgmsCompanionProductMapper> {

    /**
     * 变更状态
     * @param ids
     * @param status
     * @return
     */
    public int updateStatus(Collection<Long> ids , Integer status);

    default Map<Long, CgmsCompanionProduct> getMap(Collection<Long> ids) {
        return CollUtil.isEmpty(ids) ? new HashMap() : CollectionUtils.convertMap(this.selectByPrimaryKeys(ids), CgmsCompanionProduct::getId);
    }

    /**
     * 验证是否合法并返回
     * @param id
     * @return
     */
    default CgmsCompanionProduct validate(Long id){
        if(ObjectUtil.isNull(id) )
            throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED);
        //验证数据是否存在
        CgmsCompanionProduct bean = this.selectByPrimaryKey(id);
        if(ObjectUtil.isNull(bean) || bean.getDeltag()){
            throw new UcodeServiceException(CgmsSysResultCodeEnum.COMPANION_PRODUCT_NOT_EXITS);
        }
        return bean;
    }

    /**
     * 验证商品是否发布
     * @param id
     * @return
     */
    default CgmsCompanionProduct validateRelease(Long id){
        CgmsCompanionProduct dbBean = this.validate(id);
        if(!ObjUtil.equals(dbBean.getStatus(),  CommonReleaseStatusEnum.RELEASE.getCode())){
            throw new UcodeServiceException(CgmsSysResultCodeEnum.COMPANION_PRODUCT_NOT_RELEASE);
        }
        return dbBean;
    }

}
